
package com.shop.cloud.mall.api.controller;

import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.portal.annotation.ApiLogin;
import com.shop.cloud.mall.api.service.UserInfoService;
import com.shop.cloud.mall.api.service.UserPointsService;
import com.shop.cloud.mall.common.entity.UserInfo;
import com.shop.cloud.mall.common.entity.UserPoints;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 用户积分
 *
 * @author
 * @date 2023-11-23 10:30:39
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/userpoints")
@Tag(description = "userpoints", name = "用户积分API")
public class UserPointsController {

    private final UserPointsService userPointsService;
    private final UserInfoService userInfoService;

    /**
     * 用户积分查询
     * @param userId
     * @return R
     */
    @Operation(summary = "用户积分查询")
    @GetMapping("/{userId}")
    @ApiLogin
    public R getById(@PathVariable("userId") String userId) {
        UserPoints userPoints = userPointsService.getById(userId);
        if(userPoints == null){
            //用户积分不存在，则新增
            UserInfo userInfo = userInfoService.getById(userId);
            if(userInfo != null){
                userPoints = new UserPoints();
                userPoints.setUserId(userId);
                userPointsService.save(userPoints);
            }
        }
        return R.ok(userPoints);
    }

}
